<?php

namespace app\api\controller;
use app\api\model\Student as StudentModel;
use think\facade\Filesystem;

class Student
{
    public $studentData;
    public function __construct()
    {
        $token = input('get_token');
        if (empty($token)) output_error('token空');
        $this->studentData = StudentModel::where('member_token');
        if (empty($this->studentData))output_error('token无效');
        //判断token是否有效，通过token读取学生的所有信息
    }

    //注册功能
    public function getData()
    {
        unset($this->studentData['member_password']);
        unset($this->studentData['member_token']);
        output_data($this->studentData);
        //输出学生的所有信息，去除“student_password”字段
    }

    public function modifyName()
    {
        $name = input('get.name');
        if (empty($name))output_error('新称号不空');
        StudentModel::where('member_id','=',$this->studentData['member_id'])
            ->update(array('member_name' => $name));
        output_data(1);
        //修改学生姓名，学生姓名可重复。
    }

    public function modifyAvatar()
    {
        $file = request()->file('image');
        $savename = Filesystem::disk('avatar')->putFile('',$file,'uniqid');
        if ($this->studentData['member_avatar'] !='default.png') {
            unlink(app()->getRootPath() . 'public\\static\\upload\\avatar\\' . $this->studentData['member_avatar']);
        }
        StudentModel::where('member_id','=',$this->studentData['member_id'])
            ->update(array('member_avatar' =>$savename));
        output_data(1);
        //修改学生头像，需要自行创建文件夹，文件系统。
        //学生头像文件唯一，更新时需要删除原有的文件。
    }

}
